package com.training.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.annotation.Dao;
import com.training.admin.dao.TrainingCategoryDao;
import com.training.admin.entity.category.TrainingCategory;
import com.training.admin.service.TrainingCategoryService;

/**
 * 
 * @author Rockett
 *
 */
@Service("trainingCategoryService")
public class TrainingCategoryServiceImpl extends DataServiceImpl<TrainingCategory> implements TrainingCategoryService{



	@Dao
    private TrainingCategoryDao trainingCategoryDao;
	/* (non-Javadoc)
	 * @see com.training.admin.service.TrainingCategoryService#getAllCategory()
	 */
	@SuppressWarnings({"rawtypes", "unchecked"})
    @Override
	public Map getAllCategory() {
		List <TrainingCategory> catList = this.listAll(TrainingCategory.class);
		Map<String,String> allCatMap = new HashMap<String,String>();
		Map<String,String> allNameIdMap = new HashMap<String,String>();
		if(catList!=null && catList.size()>0){
			
			for(int i=0;i<catList.size();i++){
				TrainingCategory trainingCategory = catList.get(i);
				allNameIdMap.put(trainingCategory.getId().toString(),trainingCategory.getName());
				if(trainingCategory.getParentId()!=null){
					//二级
					String valueString = allCatMap.get(trainingCategory.getParentId().toString());
					if(valueString==null || valueString.length()==0){
						valueString = trainingCategory.getId().toString();
					}else{
						valueString = valueString + ","+trainingCategory.getId().toString();
					}
					allCatMap.put(trainingCategory.getParentId().toString(),valueString);
				}
			}
			 
			
		}
		Map rtnMap = new HashMap();
		rtnMap.put("allCatMap", allCatMap);
		rtnMap.put("allNameIdMap", allNameIdMap);
		return rtnMap;
	}
    

	/**
	 * 获取所有一级分类列表
	 * 
	 * @return
	 */
	public List<TrainingCategory> listAllFirstCategory(){
		return trainingCategoryDao.listAllFirstCategory();
	}

    @Override
	public List<TrainingCategory> listByFirstCategoryId(Long categoryId) {
    	return trainingCategoryDao.listByFirstCategoryId( categoryId);
		
	}


    /* (non-Javadoc)
     * @see com.training.admin.service.TrainingCategoryService#listFirstCategory()
     */
    public List<TrainingCategory> listFirstCategory() {
        
        List<TrainingCategory> trainingCategories = trainingCategoryDao.listFirstCategory();
        
        return trainingCategories;
    }
    
    /**
     * @return
     */
    public List<TrainingCategory> listSecondCategory(){
        
        List<TrainingCategory> trainingCategories = trainingCategoryDao.listSecondCategory();
        
        return trainingCategories;
        
    }

}
